package com.liu.SRMan.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.g4studio.core.json.JsonHelper;
import org.g4studio.core.metatype.Dto;
import org.g4studio.core.mvc.xstruts.action.ActionForm;
import org.g4studio.core.mvc.xstruts.action.ActionForward;
import org.g4studio.core.mvc.xstruts.action.ActionMapping;
import org.g4studio.core.mvc.xstruts.actions.BaseAction;
import org.g4studio.core.util.G4Constants;
import org.g4studio.core.web.BizAction;
import org.g4studio.core.web.CommonActionForm;

import com.liu.SRMan.service.HorizontalService;

public class HorizontalAction  extends BizAction {

    private HorizontalService horizontalService = (HorizontalService) getService("horizontalService");

    /*
     * 进入新建横向项目页面用action
     */
    public ActionForward newHorizontalProject(ActionMapping mapping, ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {

       return mapping.findForward("newHorizontalProject");
    }
    
    /*
     * 进入横向项目一览页面用action
     */
    public ActionForward overView(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return mapping.findForward("overView");
	}
    /*
     * 保存横向项目用action
     */
    public ActionForward saveHorizontalProject(ActionMapping mapping, ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String userdept = super.getSessionContainer(request).getUserInfo().getDeptid();
       String userid = super.getSessionContainer(request).getUserInfo().getUserid();

       CommonActionForm aForm = (CommonActionForm) form;
       Dto inDto = aForm.getParamAsDto(request);
       
       inDto.put("indate", inDto.getAsTimestamp("indate"));
       inDto.put("userid", userid);
       inDto.put("userdept", userdept);
       Dto outDto = horizontalService.saveHorizontalItem(inDto);
       
       outDto.put("success", new Boolean(true));
       outDto.put("msg", "横向项目数据保存成功!");
       write(JsonHelper.encodeObject2Json(outDto), response);
       return mapping.findForward(null);
    }
    /*
     * 横向项目检索用action
     */
    public ActionForward queryHorizontalInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto dto = aForm.getParamAsDto(request);
		
		List list = g4Reader.queryForPage("Temp.queryHorizontal", dto);
		Integer countInteger = (Integer) g4Reader.queryForObject("Temp.countHorizontal", dto);
		System.out.println("接收到的表单提交参数：\n" + dto);
		String jsonString = JsonHelper.encodeList2PageJson(list, countInteger, G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
    
    /**
	 * 更新横向项目
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateHorizontal(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm cForm = (CommonActionForm) form;
		Dto inDto = cForm.getParamAsDto(request);
		
		horizontalService.updateHorizontal(inDto);
		setOkTipMsg("数据修改成功", response);
		return mapping.findForward(null);
	}
	/**
	 * 删除横向项目
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteHorizontal(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CommonActionForm aForm = (CommonActionForm) form;
		Dto inDto = aForm.getParamAsDto(request);
		horizontalService.deleteHorizontal(inDto);
		setOkTipMsg("横向项目删除成功", response);
		return mapping.findForward(null);
	}
	
	public ActionForward formSynForwardMumber(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response)throws Exception{
		CommonActionForm aForm = (CommonActionForm)form;
		Dto inDto = aForm.getParamAsDto(request);
		request.setAttribute("value", inDto.toJson());
		System.out.println("接收到的表单提交参数：\n" + inDto);
		return mapping.findForward("formSynForwardMumber");
	}
}